package net.qanar.gwnewsfeed.feed;

import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

import net.qanar.gwnewsfeed.*;
import net.qanar.gwnewsfeed.feed.swing.*;

public class Feed implements ActionListener, ItemListener{
	private ArrayList<Item> items;
	private ArrayList<JCheckBoxMenuItem> optionsList;
	private ArrayList<net.qanar.gwnewsfeed.reader.Reader> readers;
	
	private JFrame frame;
	
	public void run() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } catch (Exception e) {}
		
		items = new ArrayList<Item>();	
		optionsList = new ArrayList<JCheckBoxMenuItem>();
		readers = new ArrayList<net.qanar.gwnewsfeed.reader.Reader>();
		
		frame = new JFrame("Guild Wars News Reader");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setBounds(0, 0, 800, 600);	    
	    createMenu();	    
	    calculateList();
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("Quit"))
			System.exit(0);
		else if (arg0.getActionCommand().equals("About")) {
			about();		    
		}
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		calculateList();
		frame.setVisible(true);
	}
	
	private void about() {
		JFrame about = new JFrame("About");
		about.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		about.setSize(new Dimension(200, 150));
		about.getContentPane().setLayout(new FlowLayout());
		
		JLabel l1 = new JLabel("Author: \tNiek 'Qanar' Bergans", JLabel.LEFT);
		l1.setPreferredSize(new Dimension(190,20));
		JLabel l2 = new JLabel("Version: \t0.4\n", JLabel.LEFT);
		l2.setPreferredSize(new Dimension(190,20));
		JLabel l3 = new JLabel("Date: \t June 9th 2008", JLabel.LEFT);
		l3.setPreferredSize(new Dimension(190,20));
		
		about.getContentPane().add(l1);
		about.getContentPane().add(l2);
		about.getContentPane().add(l3);
		about.setVisible(true);
	}

	private void calculateList() {
		items.removeAll(items);
		
		for (int i = 0; i < optionsList.size(); i++) {
			if (((JCheckBoxMenuItem)optionsList.toArray()[i]).isSelected()) {
				items.addAll(((net.qanar.gwnewsfeed.reader.Reader)readers.toArray()[i]).getItems());
			}
		}
		
	    Collections.sort(items);
				
	    JScrollPane sp = new JScrollPane(new StandardGUI(items));
	    sp.setSize(new Dimension(750,525));
		frame.setContentPane(sp);
	}

	private void createMenu() {
		JMenuItem close = new JMenuItem("Quit");
	    close.addActionListener(this);
		JMenuItem about = new JMenuItem("About");
		about.addActionListener(this);
		
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("setup.ini")));
			String line = br.readLine();
			
			while (line != null) {
				Date d = new Date();
				Object r = Class.forName("net.qanar.gwnewsfeed.reader."+line).getConstructor().newInstance(); 
				readers.add((net.qanar.gwnewsfeed.reader.Reader)r);
				optionsList.add(new JCheckBoxMenuItem(((net.qanar.gwnewsfeed.reader.Reader)r).getName()));
				System.out.println("Loaded " + ((net.qanar.gwnewsfeed.reader.Reader)r).getName() + ": "+ (new Date().getTime() - d.getTime()) / 1000 + " second(s)");
				line = br.readLine();
			}
			for (JCheckBoxMenuItem i : optionsList) {
				i.setSelected(true);
				i.addItemListener(this);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JMenu main = new JMenu("File");
		main.add(close);
		
	    JMenu options = new JMenu("Options");
	    for (JCheckBoxMenuItem i : optionsList) {
	    	options.add(i);
	    }
	    
	    JMenu help = new JMenu("Help");
	    help.add(about);
		
		JMenuBar menuBar = new JMenuBar();
	    menuBar.add(main);
	    menuBar.add(options);
	    menuBar.add(help);
	    
	    frame.setJMenuBar(menuBar);
	}
	
	public static void startBrowser(URL url) {
		try {
			Runtime.getRuntime().exec("cmd.exe /c start " + url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
